﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using CashOrCard.Models;

namespace CashOrCard.Helper
{
    public class PermissionUtility
    {
        public static IList<dynamic> Menus
        {
            get
            {

                var menus = new[]
                {
                    new { LinkText="Basic Management", ActionName="Index",ControllerName="",PermissionName="",PageID=1, ParentID="", IsFolder="True"},
                    new { LinkText="User Management", ActionName="Index",ControllerName="User",PermissionName="usermanagement_access",PageID=2, ParentID="1", IsFolder="False"}, 
                    new { LinkText="Site Pages", ActionName="Index",ControllerName="SitePage",PermissionName="sitepagemanagement_access",PageID=3, ParentID="1", IsFolder="False"},

                    new { LinkText="Business Management", ActionName="Index",ControllerName="",PermissionName="",PageID=4, ParentID="", IsFolder="True"},
                    new { LinkText="Business Sites", ActionName="Index",ControllerName="AdvtAdmin",PermissionName="sitemanagement_access",PageID=5, ParentID="4", IsFolder="False"},                    
                    new { LinkText="Fake Comment", ActionName="FackComment",ControllerName="PromotionComment",PermissionName="fcmanagement_access",PageID=6, ParentID="4", IsFolder="False"},
                    
                    new { LinkText="Voucher Center", ActionName="Index",ControllerName="",PermissionName="",PageID=7, ParentID="", IsFolder="True"},
                    new { LinkText="Vourcher", ActionName="Index",ControllerName="PayPal",PermissionName="paypalprepaid_access",PageID=8, ParentID="7", IsFolder="False"},
                    //new { LinkText="Recharge", ActionName="Recharge",ControllerName="User",PermissionName="recharge_access",PageID=8, ParentID="7", IsFolder="False"},
                    new { LinkText="Payment History", ActionName="Index",ControllerName="PaymentHistory",PermissionName="paymenthistory_access",PageID=9, ParentID="7", IsFolder="False"},

                    new { LinkText="My Account", ActionName="Index",ControllerName="",PermissionName="",PageID=10, ParentID="", IsFolder="True"},
                    new { LinkText="Basic Profile", ActionName="MyProfile",ControllerName="User",PermissionName="myprofile_access",PageID=11, ParentID="10", IsFolder="False"},
                    new { LinkText="Change Password", ActionName="ChangePassword",ControllerName="User",PermissionName="password_access",PageID=12, ParentID="10", IsFolder="False"},
                    new { LinkText="Comments", ActionName="CustomerComment",ControllerName="PromotionComment",PermissionName="customercomment_access",PageID=13, ParentID="10", IsFolder="False"}
                };

                return menus.ToList<dynamic>();
            }
        }

        public static IList<Menu> GenerateMenuDataList()
        {
            var menus = Menus;
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {

                String[] permissions = Roles.GetRolesForUser();

                //Get All Authorized Menus,including parent and child
                IList<dynamic> authorizedMenus = new List<dynamic>();
                foreach (dynamic item in menus)
                {
                    bool isFolder = false;
                    bool.TryParse(item.IsFolder, out isFolder);
                    if (isFolder)
                    {
                        authorizedMenus.Add(new { LinkText = item.LinkText, ActionName = item.ActionName, ControllerName = item.ControllerName, PermissionName = item.PermissionName, PageID = item.PageID, ParentID = item.ParentID, IsFolder = item.IsFolder });
                        continue;
                    }
                    string[] permissionsArr = item.PermissionName.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string permission in permissionsArr)
                    {
                        if (permissions.Contains(permission))
                        {
                            authorizedMenus.Add(new { LinkText = item.LinkText, ActionName = item.ActionName, ControllerName = item.ControllerName, PermissionName = item.PermissionName, PageID = item.PageID, ParentID = item.ParentID, IsFolder = item.IsFolder });
                            break;
                        }
                    }
                }
             
                //Get Parent Menus
                var parentLinks = from item in authorizedMenus
                                  where item.ParentID.Equals(string.Empty)
                                  orderby item.PageID ascending
                                  select item;


                IList<Menu> menuList = new List<Menu>();

                int index = 0;
                foreach (var parentLink in parentLinks)
                {
                    string navStr = string.Empty;
                    if (!string.IsNullOrEmpty(parentLink.ControllerName) && !string.IsNullOrEmpty(parentLink.ActionName))
                    {
                        navStr = "/" + parentLink.ControllerName + "/" + parentLink.ActionName;
                    }

                    //Get child menus
                    var childLinks = from item in authorizedMenus
                                     where item.ParentID.Equals(parentLink.PageID.ToString())
                                     orderby item.PageID ascending
                                     select item;
                    IList<Menu> childMenuList = new List<Menu>();
                    if (bool.Parse(parentLink.IsFolder) && childLinks.Count() == 0)
                    {
                        continue;
                    }
                    foreach (var childLink in childLinks)
                    {
                        string childNavStr = string.Empty;
                        if (!string.IsNullOrEmpty(childLink.ControllerName) && !string.IsNullOrEmpty(childLink.ActionName))
                        {
                            childNavStr = "/" + childLink.ControllerName + "/" + childLink.ActionName;
                        }
                        Menu childMenu = new Menu() { Text = childLink.LinkText, NavLink = childNavStr };
                        childMenuList.Add(childMenu);
                    }
                    Menu menu;
                    if (index == 0)
                    {
                        menu = new Menu() { Text = parentLink.LinkText, NavLink = navStr, SubMenus = childMenuList, IsDefault=true };
                    }
                    else
                    {
                        menu = new Menu() { Text = parentLink.LinkText, NavLink = navStr, SubMenus = childMenuList };
                    }
                    menuList.Add(menu);

                    index++;
                }

                return menuList;
            }
            else
            {   
                return null;
            }
        }
    }
}